const contrastKey = "userCollegeContrast"
const collectKey = "userCollegeCollect"
const provinceListKey = "provinceListkey"
const selProvinceKey = "selProvinceKey"
const volunteerInfoKey = "volunteerInfokey"
const collectHistroyKey = "searchCollegeHistory"
const xuanKeInfoKey = "xuanKeInfoKey"
const pingInfoKey = "pingInfoKey"
const seejobDataKey = "seejobDataKey"
//专业收藏
const collectMajorKey = "userCollectMajor"
//职业收藏
const collectJobKey = "userCollectJob"
// 提前批搜索历史
const tiqianpiHistoryKey = "tiqianpiHistory"
const historyUserLoginInfoKey = "getHistoryUserLoginInfo"

// 获取本地缓存
function getStorageSync(key) {
	const value = uni.getStorageSync(key)
	return value // ? value:[]
}

// 异步设置本地缓存
function setStorage(data, key) {
	uni.setStorage({
		key,
		data
	})
}



// 同步设置本地缓存
function setStorageSync(data, key) {
	uni.setStorageSync(key, data)
}

let api = {
	/**
	 * @description 微信分享函数
	 * @param {Object} url 跳转地址
	 * @param {Object} title 分享标题
	 * @param {Object} img 图片链接
	 */
	WXshare(url, title, img, summary) {
		if (!img) {
			img = "https://mcdn.chatgk.com/xcx/static/images/wode/wodeapplogo.png"
		}
		// uni.showToast({
		// 	title: "功能暂未开放",
		// 	icon: "none"
		// })
		// return
		uni.share({
			provider: "weixin",
			scene: "WXSceneSession",
			type: 0,
			href: url,
			title: title,
			summary: summary,
			imageUrl: img,
			success: function(res) {},
			fail: function(err) {}
		});

	},
	/**
	 * @description 微信朋友圈分享函数
	 * @param {Object} url 跳转地址
	 * @param {Object} title 分享标题
	 * @param {Object} img 图片链接
	 */
	WXClrcleShare(url, title, img, summary) {
		if (!img) {
			img = "https://mcdn.chatgk.com/xcx/static/images/wode/wodeapplogo.png"
		}
		// uni.showToast({
		// 	title: "功能暂未开放",
		// 	icon: "none"
		// })
		// return

		uni.share({
			provider: "weixin",
			scene: "WXSceneTimeline",
			type: 0,
			href: url,
			title: title,
			summary: summary,
			imageUrl: img,
			success: function(res) {},
			fail: function(err) {}
		});
	},
	/**
	 * @description QQ分享函数
	 * @param {Object} url 跳转地址
	 * @param {Object} title 分享标题
	 * @param {Object} img 图片链接
	 */
	QQShare(url, title, img, summary) {
		if (!img) {
			img = "https://mcdn.chatgk.com/xcx/static/images/wode/wodeapplogo.png"
		}
		// uni.showToast({
		// 	title: "功能暂未开放",
		// 	icon: "none"
		// })
		// return

		uni.share({
			provider: "qq",
			type: 0,
			href: url,
			title: title,
			summary: summary,
			// imageUrl: 'http://img.htmlsucai.com/forum/201703/17/130549n1hz131qqpp2cb3n.jpg',
			imageUrl: img,
			success: function(res) {},
			fail: function(err) {}
		});
	},
	/**
	 * @description 复制链接分享
	 * @param {Object} url 跳转地址
	 * @param {Object} title 分享标题
	 * @param {Object} img 图片链接
	 */
	COPYShare(url, title, img) {
		return new Promise((resolve, reject) => {
			if (!img) {
				img = "https://mcdn.chatgk.com/xcx/static/images/wode/wodeapplogo.png"
			}

			uni.setClipboardData({
				data: url,
				success() {
					resolve()
				},
				fail(err) {
					reject(err)
				}
			})
		})
	},
	/*
	 *  ================学校对比=================
	 */
	getContrastList: () => getStorageSync(contrastKey),
	setContrastList: data => setStorage(data, contrastKey),
	/*
	 *  ================学校收藏=================
	 */
	getCollectList: () => getStorageSync(collectKey),
	setCollectList: data => setStorage(data, collectKey),
	/*
	 *  ================省列表=================
	 */
	getProvinceList: () => getStorageSync(provinceListKey),
	setProvinceList: data => setStorage(data, provinceListKey),
	getProvinceInfo: () => getStorageSync(selProvinceKey),
	setProvinceInfo: data => setStorageSync(data, selProvinceKey),
	/*
	 *  ================填报志愿=================
	 */
	// 获取当前填报志愿表缓存
	getVolunteerInfo: () => getStorageSync(volunteerInfoKey),
	// 设置当前填报志愿表缓存
	setVolunteerInfo: data => setStorageSync(data, volunteerInfoKey),

	/*
	 *  ================用户相关信息=================
	 */
	// 用户是否登录 reutrn bool
	isLogin() {
		if (!getApp().globalData.userInfo) {
			uni.navigateTo({
				url: '/pages/login/login-home'
			})
			return false
		}

		return true
	},
	// 获取用户科类信息 reutrn string
	getFormatFamilyName() {
		const userInfo = getApp().globalData.userInfo
		if (!userInfo) {
			return ""
		}

		const familyInfos = {
			"11": "理科",
			"22": "文科",
			"1": "物",
			"2": "化",
			"3": "生",
			"6": "政",
			"4": "史",
			"5": "地",
			"7": "技"
		}

		if (!userInfo.sgid) {
			return ""
		}
		const familyIDs = userInfo.sgid.split(',')

		let string = ""
		for (let id of familyIDs) {
			if (string != "") {
				string = string + '/'
			}
			string = string + familyInfos[id]
		}

		return string
	},

	/*
	 *  ================搜索大学历史记录=================
	 */
	getCollegeHistroy: () => getStorageSync(collectHistroyKey),
	setCollegeHistroy: data => setStorage(data, collectHistroyKey),
	/*
	 *  ================选择助手=================
	 */
	getXuanKeInfo: () => getStorageSync(xuanKeInfoKey),
	setXuanKeInfo: data => setStorage(data, xuanKeInfoKey),
	/*
	 *  ================专业评测=================
	 */
	getPingInfo: () => getStorageSync(pingInfoKey),
	setPingInfo: data => setStorage(data, pingInfoKey),
	/*
	 *  ================查职业数据=================
	 */
	getSeejobData: () => getStorageSync(seejobDataKey),
	setSeejobData: data => setStorage(data, seejobDataKey),
	/*
	 *  ================专业收藏=================
	 */
	getCollectMajorList: () => getStorageSync(collectMajorKey),
	setCollectMajorList: data => setStorage(data, collectMajorKey),
	/*
	 *  ================职业收藏=================
	 */
	getCollectJobList: () => getStorageSync(collectJobKey),
	setCollectJobList: data => setStorage(data, collectJobKey),

	/*
	 *  ================提前批搜索历史=================
	 */
	getTiqianpiHistoryList: () => getStorageSync(tiqianpiHistoryKey),
	setTiqianpiHistoryList: data => setStorage(data, tiqianpiHistoryKey),

	/*
	 *  ================历史登录用户数据=================
	 */
	getHistoryUserLoginInfo: () => getStorageSync(historyUserLoginInfoKey),
	setHistoryUserLoginInfo: data => setStorage(data, historyUserLoginInfoKey),
}

export default api;